#!/usr/local/bin/python
#coding=utf-8
__author__ = 'xiang'

from email.mime.text import MIMEText
import smtplib
import os

def send_mail(to_list, cc_list, html, sub):
    me = mail_user
    msg = MIMEText(html, _subtype='html', _charset='utf-8')  # 格式化邮件内容为html，编码为utf-8
    msg['Subject'] = sub    # 邮件主题
    msg['From'] = me    # 发件人
    msg['To'] = ";".join(to_list)  # 收件人，将列表转换为字符串
    msg['Cc'] = ";".join(cc_list)  # 抄送人，将列表转换为字符串
    try:
        send_smtp = smtplib.SMTP_SSL()    # 实例化
        send_smtp.connect(mail_host,465)    # 连接smtp服务器,ssl 465端口
#	send_smtp=smtplib.SMTP()
#	send_smtp.connect(mail_host) 非ssl ，25端口
        send_smtp.login(mail_user, mail_pass)    # 使用定义的账号密码进行登录
        send_smtp.sendmail(me, to_list+cc_list, msg.as_string())    # 发送邮件
        send_smtp.close()    # 关闭连接
        return True
    except Exception, e:
        # logging.debug(e)
        print e
        return False

if __name__ == '__main__':
    mail_host = 'xxxx'
    mail_user = "xxxx"
    mail_pass = "xx"
    mailto_list = ["xx@qq.com",'xx@qq.com']
    mailcc_list = ['xx@163.com']
    html = "<h1> hello world this is test!</h1>"
    sub = "send mail test"
    if send_mail(mailto_list, mailcc_list, str(html), sub):
        print "Send mail succed!"
    else:
        print "Send mail failed"
